﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MobSoulWPhoneToolkit.Controls
{
    public partial class NumericUpDown : UserControl
    {
        public NumericUpDown()
        {
            InitializeComponent();
        }
        public bool IsSigned = true;

        private long value = 0;
        public long Value
        {
            get { return this.value; }
            set
            {
                if (IsSigned)
                {
                    this.value = value;
                    if (ValueChanged != null)
                        ValueChanged(this, new RoutedEventArgs());
                }
                else
                {
                    if (value >= 0)
                    {
                        this.value = value;
                        if (ValueChanged != null)
                            ValueChanged(this, new RoutedEventArgs());
                    }
                    else
                    { value = 0; }
                }
                number.Text = this.value.ToString();
            }
        }

        public event RoutedEventHandler ValueChanged;
        private void plusButton_Click(object sender, RoutedEventArgs e)
        {
            Value++;
        }

        private void plusButton_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            pressedPlus.Visibility = Visibility.Collapsed;
        }

        private void plusButton_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            pressedPlus.Visibility = Visibility.Visible;
        }

        private void minusButton_Click(object sender, RoutedEventArgs e)
        {
            Value--;
        }

        private void minusButton_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            pressedMinus.Visibility = Visibility.Collapsed;
        }

        private void minusButton_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            pressedMinus.Visibility = Visibility.Visible;
        }

        private void number_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                value = Convert.ToInt64(number.Text.ToString());

            }
            catch (Exception)
            { }
        }

    }
}
